package com.wl.index.Biz.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wl.entity.Comments;
import com.wl.index.Biz.ICommentsBiz;
import com.wl.index.Mapper.CommentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class CommentBizImpl extends ServiceImpl<CommentMapper, Comments> implements ICommentsBiz {
    @Autowired
    private CommentMapper commentMapper;

    //通过帖子id查询评论信息
    @Override
    public List<Comments> getCommentsByNid(Integer nid) {
        System.out.println("正在查询nid为: " + nid + " 的评论");
        List<Comments> comments = commentMapper.getCommentsByNid(nid);
        System.out.println("查询结果: " + comments);
        System.out.println("结果数量: " + comments.size());
        return comments;
    }

    @Override
    public List<Map<String, Object>> getCommentsByUid(Integer nid) {
        System.out.println("正在查询nid为: " + nid + " 的评论及用户信息");
        List<Map<String, Object>> comments = commentMapper.getCommentsByUid(nid);
        System.out.println("查询结果: " + comments);
        System.out.println("结果数量: " + comments.size());
        return comments;
    }

    //添加评论信息
    @Override
    public int addComment(Comments comments) {
        if (comments == null || comments.getUid() == null) {
            throw new IllegalArgumentException("缺少必要参数");
        }
        // 如果creatAt为null，设置为当前时间
        if(comments.getCreatAt() == null) {
            comments.setCreatAt(new Date());
        }
        return commentMapper.addComment(comments);
    }
}
